<?php if ( ! defined('WDK_PATH')) exit('No direct script access allowed');
require_once PROJECT_PATH.'core/AdminController.php';
class Post extends AdminController
{
	private $bbs;
	function __construct()
	{
		parent::__construct();
	}
	
	protected function _init() 
	{
		parent::_loginCheck();
	}
	
	public function index($bbs_seq=0)
	{	
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$assign_data = array();
		/****************************************************************************************************************************/
		try
		{
			$assign_data['json'] = $this->json(true);
			$assign_data['bbs_seq'] = $bbs_seq;
			/****************************************************************************************/
			if($bbs_seq > 0)
			{
				$bbs = Wdk::get('Bbs');
				$bbs->load($bbs_seq);
				$assign_data['bbs'] = $bbs;
			}
			/****************************************************************************************/
			$bm = Wdk::get('BbsManager');
			$bm->loadAll();
			$assign_data['bbs_list'] = $bm['lists']; 
			/****************************************************************************************/
		}
		catch(WException $e)
		{
			parent::_error($e);
		}
		/*****************************************************************************************************************************
		* RESULT
		*****************************************************************************************************************************/
		parent::_view($assign_data);
		/****************************************************************************************************************************/
	}
	
	public function json($return=false)
	{
		/*****************************************************************************************************************************
		 * POST PARAMS..
		*****************************************************************************************************************************/
		$page		= (int)		post('page', 1);
		$sf			= (string)	post('sf', '');
		$sk			= (string)	post('sk', '');
		$fk_bbs_seq	= (int)	post('fk_bbs_seq', 0);
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$bbs = Wdk::get('Bbs');
		if($fk_bbs_seq > 0)
		{
			$bbs->load($fk_bbs_seq);
		}
		$listsize	= $bbs['bbs_listsize'];
		$pagesize	= $bbs['bbs_pagesize'];
		$lm = Wdk::get('BbsPostManager');
		$lists = array();
		$where = array();
		$assign_data = array('listsize'=>$listsize,'pagesize'=>$pagesize);
		/****************************************************************************************************************************/
		try
		{
			if($fk_bbs_seq > 0)
			{
				$where[] = "`fk_bbs_seq` = {$fk_bbs_seq}";
			}
			if(strlen($sf) > 0 && strlen($sk) > 0)
			{
				$where[] = "`{$sf}` LIKE '{$sk}'";
			}
			$lm['listsize'] = $listsize;
			$lm['page'] = $page;
			$lm['where'] = $where;
			$lm->load();
			foreach($lm['lists'] as $idx=>$list)
			{
				$data = array(	'no'				=>	$lm['total'] - ($page-1)*$listsize - $idx,
								'seq'				=>	$list['bbs_post_seq'],
								'category_name'		=>	$list['category_nane'],
								'user_name'			=>	$list['bbs_post_user_name'],
								'title'				=>	$list['bbs_post_title'],
								'hits'				=>	$list['bbs_post_hits'],
								'secure_flag'		=>	$list['bbs_post_recommend_flag'],
								'notice_flag'		=>	$list['bbs_post_use_flag'],
								'insert_time'		=>	date('Y-m-d H:i:s', $list['bbs_post_insert_time'])
							);
				array_push($lists, $data);
			}
			$assign_data['total'] = $lm['total'];
			$assign_data['lists'] = $lists;
		}
		catch(NoRecordException $e)
		{
			$assign_data['total'] = 0;
			$assign_data['lists'] = array();
		}
		catch(WException $e)
		{
			$assign_data['error'] = true;
			$assign_data['msg'] = "Error";
		}
		/*****************************************************************************************************************************
		 * RESULT
		*****************************************************************************************************************************/
		return parent::_json($assign_data, $return);
		/****************************************************************************************************************************/
	}
	
	public function edit($seq=0, $bbs_seq=0) 
	{
		$this->__edit();
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$assign_data = array();
		$obj = Wdk::get('BbsPost');
		/****************************************************************************************************************************/
		try
		{
			$obj['fk_bbs_seq'] = $bbs_seq;			
			if((int)$seq > 0)
			{
				$obj->load($seq);
			}
			$assign_data['data'] = $obj;
			/****************************************************************************************/
			$bm = Wdk::get('BbsManager');
			$bm->loadAll();
			$assign_data['bbs_list'] = $bm['lists'];
			/****************************************************************************************/
			$bbs = Wdk::get('Bbs');
			if($bbs_seq > 0)
			{
				$bbs->load($bbs_seq);
			}
			$assign_data['bbs'] = $bbs;
			/****************************************************************************************/
		}
		catch(NoRecordException $e)
		{
			parent::_error($e);
		}
		catch(WException $e)
		{
			parent::_error($e);
		}
		/*****************************************************************************************************************************
		* RESULT
		*****************************************************************************************************************************/
		parent::_layout('admin_popup');
		parent::_view($assign_data);
		/****************************************************************************************************************************/
	}
	
	private function __edit() 
	{
		if(parent::_isPost() == false) return;
		/*****************************************************************************************************************************
		 * BASIC VARS
		*****************************************************************************************************************************/
		$assign_data = array();
		$obj = Wdk::get('BbsPost');
		/****************************************************************************************************************************/
		try
		{
			$seq = post('bbs_post_seq', 0);
			if($seq > 0)
			{
				$obj->load($seq);
			}
			
			$obj['fk_bbs_seq']				= (int) post('fk_bbs_seq');
			$obj['fk_category_seq']			= (int) post('fk_category_seq');
			$obj['fk_user_seq']				= $this->session['user_se'];
			$obj['bbs_post_user_name']		= (string) post('bbs_post_user_name');
			$obj['bbs_post_user_email']		= (string) post('bbs_post_user_email');
			$obj['bbs_post_user_mobile']	= (string) post('bbs_post_user_mobile');
			$obj['bbs_post_user_passwd']	= (string) post('bbs_post_user_passwd');
			$obj['bbs_post_title']			= (string) post('bbs_post_title');
			$obj['bbs_post_content']		= (string) post('bbs_post_content');
			$obj['bbs_post_files']			= (string) post('bbs_post_files');
			$obj['bbs_post_secure_flag']	= (string) post('bbs_post_secure_flag', 'F');
			$obj['bbs_post_notice_flag']	= (string) post('bbs_post_notice_flag', 'F');
			$obj->autoUpdate();
		}
		catch(NoRecordException $e)
		{
			parent::_error($e);
		}
		catch(WException $e)
		{
			parent::_error($e);
		}
		/*****************************************************************************************************************************
		* RESULT
		*****************************************************************************************************************************/
		parent::_script(array(	"alert('저장완료!')",
								"opener.wlm.reload();",
								"window.close();"
							)
						);
		/****************************************************************************************************************************/		
	}

	public function del() 
	{
		parent::_del(Wdk::get('BbsPost'));
	}
}